/**
 * 描述: 检修项目列表页
 *
 * @author chengguang
 * @since 2021/11/19 14:45
 */
layui.define(['index', 'form', 'table', 'laypage', 'layedit'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    var table = layui.table;
    var laypage = layui.laypage;
    var layedit = layui.layedit;

    table.init('zmn-table', {
        toolbar: "#zmn-table-tool",
        page: true,
        limit: json.pageSize,
        height: 'full-149',
        defaultToolbar: ['filter']
    });
    laypage.render(layui.$.extend({limits: [10, 20, 30, 40, 50, 100]}, zmnPage.laypageOptions(json)));

    let ids = new Array();

    // 选择（全选或单选）
    table.on("checkbox(zmn-table)", function (obj) {
        ids = table.checkStatus('zmn-table').data.map((item) => item.maintainId);
    });

    // 启用/停用 新增
    table.on("toolbar(zmn-table)", function (obj) {
        if (obj.event === "add") {
            let title = '新增检修项目';
            layer.open({
                type: 2,
                offset: '15%',
                area: ['690px', '550px'],
                title: [title],
                content: ["/base/maintainItem/addEditView.action", 'no'],
                end: function () {
                    window.location.reload();
                }
            });
        }
        //启用停用
        if (obj.event == "start" || obj.event == "stop") {
            if (ids.length === 0) {
                return layer.alert("请选择至少1条数据！");
            }
            zmnPage.layStartStop(obj, "/base/maintainItem/updateStatus.action", ids.join(","));
        }
    });

    // 启用停用 checkbox
    form.on("switch(chk_one_status)", function (obj) {
        let url = "/base/maintainItem/updateStatus.action";
        let servId = obj.elem.getAttribute("key");
        let status = obj.elem.checked ? 2 : 1;
        switch (status) {
            case 1:
                zmnPage.ajaxSimple(url, {ids: servId, status: 1}, 1000);
                break;
            case 2:
                zmnPage.ajaxSimple(url, {ids: servId, status: 2}, 1000);
                break;
        }
        setTimeout(function () {
            location.reload(true);
        }, 3000);
    });
    // 修改
    $("body").on("click", ".btn-operate", event => {
        let maintainId = $(event.target).data("id");
        let title = '修改检修项目';
        layer.open({
            type: 2,
            offset: '15%',
            area: ['700px', '600px'],
            title: [title],
            content: ["/base/maintainItem/addEditView.action?maintainId=" + maintainId, 'no'],
            end: function () {
                window.location.reload();
            }
        });

        return false;
    });
    // 排序
    table.on("edit(zmn-table)", function (obj) {
        zmnPage.laySort(obj, "/base/maintainItem/updateSort.action", obj.data.maintainId);
        setTimeout(function () {
            location.reload(true);
        }, 3000);
    });

    //对外暴露的接口
    exports('plat_maintainItem_list', {});
});

